// 基于immutable统一处理shouldComponentUpdate
import { Component } from "react";
import { is } from "immutable";

class BaseComponent extends Component {
  constructor(props, context, updater) {
    super(props, context, updater);
    this.state = {};
  }

  shouldComponentUpdate(nextProps, nextState) {
    const thisProps = this.props || {};
    const thisState = this.state || {};
    nextProps = nextProps || {};
    nextState = nextState || {};
    if (
      Object.keys(thisProps).length !== Object.keys(nextProps).length ||
      Object.keys(thisProps).length !== Object.keys(nextState).length
    ) {
      return true;
    }
    for (const key in nextProps) {
      if (!is(thisProps[key], nextProps[key])) {
        return true;
      }
    }
    for (const key in nextState) {
      if (!is(thisState[key], nextState[key])) {
        return true;
      }
    }
    return false;
  }
}

export default BaseComponent;
